package com.qys.livesMall.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.exception.enums.ExceptionEnum;
import com.qys.livesMall.system.modal.dto.ThinkTanksTypeQuery;
import com.qys.livesMall.system.modal.entity.SysThinkTanksType;
import com.qys.livesMall.system.mapper.SysThinkTanksTypeMapper;
import com.qys.livesMall.system.service.SysThinkTanksInfoService;
import com.qys.livesMall.system.service.SysThinkTanksTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 平台智库类型 服务实现类
 * </p>
 *
 * @author
 * @since 2023-10-31
 */
@Service
public class SysThinkTanksTypeServiceImpl extends ServiceImpl<SysThinkTanksTypeMapper, SysThinkTanksType> implements SysThinkTanksTypeService {

    @Autowired
    private SysThinkTanksInfoService thinkTanksInfoService;
    @Override
    public List<SysThinkTanksType> getAll(ThinkTanksTypeQuery query) {
        LambdaQueryWrapper<SysThinkTanksType> wq=new LambdaQueryWrapper<>();
        if (query.getTypeId()!=null){
            wq.eq(SysThinkTanksType::getId,query.getTypeId());
        }
        if (StringUtils.isNotBlank(query.getTypeName())){
            wq.like(SysThinkTanksType::getTypeName,query.getTypeName());
        }
        if (StringUtils.isNotBlank(query.getKeyWord())){
            wq.like(SysThinkTanksType::getKeyWord,query.getKeyWord());
        }
        return list(wq);
    }

    @Override
    public void add(SysThinkTanksType thinkTanksType) {
        if (StringUtils.isBlank(thinkTanksType.getTypeName())){
            throw new QysException(ExceptionEnum.PARAM_IS_ERROR);
        }
        save(thinkTanksType);
    }

    @Override
    public void edit(SysThinkTanksType thinkTanksType) {
        if (thinkTanksType.getId()==null){
            throw new QysException(ExceptionEnum.PARAM_IS_ERROR);
        }
        updateById(thinkTanksType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(Integer id) {
        if (id==null){
            throw new QysException(ExceptionEnum.PARAM_IS_ERROR);
        }
        removeById(id);
        //删除问题
        thinkTanksInfoService.delByType(id);
    }
}
